<!--
	This file is part of ELCube.
	ELCube is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	ELCube is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.
	You should have received a copy of the GNU Affero General Public License
	along with ELCube.  If not, see <https://www.gnu.org/licenses/>.
-->
<template>
    <nk-card @expand="$nkSortableVxeTable">
        <vxe-toolbar v-if="editMode">
            <template v-slot:buttons>
                <vxe-button icon="fa fa-plus" status="perfect" size="mini" @click="xTableAdd()">新增</vxe-button>
            </template>
        </vxe-toolbar>
        <vxe-table
                ref="xTable"
                row-key
                auto-resize
                keep-source
                resizable
                highlight-hover-row
                show-header-overflow="tooltip"
                show-overflow="tooltip"
                size="mini"
                border=inner
                :data="list"
                :edit-config="{trigger: 'click', mode: 'row', showIcon: editMode, showStatus: true}"
                @edit-actived="xTableEditActived"
                @edit-closed="xTableEditClosed">
            <vxe-column field="program"         width="10%" title="程序" :edit-render="{
                                                                                name:'$select',
                                                                                props:{options},
                                                                                events:{change:programChange}}"
                        :formatter="['nkFromList',options]"></vxe-column>
            <vxe-column field="activeDate"      width="12%" title="激活日期" formatter="nkDatetime" :edit-render="{}">
                <template v-slot:edit="{row}">
                    <nk-a-date-picker size="small" v-model="row.activeDate" @change="itemChange"></nk-a-date-picker>
                </template>
            </vxe-column>
            <vxe-column field="effectiveDate"   width="25%" title="有效期间" :edit-render="{enabled:activedDef.effectiveDate}">
                <template v-slot="{row}">
                    <span v-if="row.effectiveDate&&row.effectiveDate.length">
                        {{row.effectiveDate[0] | nkDatetime('YYYY/MM/DD')}}
                        -
                        {{row.effectiveDate[1] | nkDatetime('YYYY/MM/DD')}}
                    </span>
                </template>
                <template v-slot:edit="{row}">
                    <nk-a-range-picker size="small" v-model="row.effectiveDate" @change="itemChange"></nk-a-range-picker>
                </template>
            </vxe-column>
            <vxe-column field="effectiveFrom"   width="10%" title="期次自" :edit-render="{enabled:activedDef.effectiveFromTo,name:'nkNumber',props:{type:'number',min:1},events:{change:itemChange}}"></vxe-column>
            <vxe-column field="effectiveTo"     width="10%" title="期次至" :edit-render="{enabled:activedDef.effectiveFromTo,name:'nkNumber',props:{type:'number',min:1},events:{change:itemChange}}"></vxe-column>
            <vxe-column field="value"           width="20%" align="right" title="值"
                                                :edit-render="{enabled:activedDef.pay || activedDef.rate || activedDef.value}">
                <template v-slot="{row}">
                    <span v-if="row._def.pay"   >{{row.pay | nkCurrency}}</span>
                    <span v-if="row._def.rate"  >{{row.rate | nkPercent('%0.0000')}}</span>
                    <span v-if="row._def.value" >{{row.value}}</span>
                </template>
                <template v-slot:edit="{row}">
                    <a-input-number size="small" v-if="row._def.pay"   v-model="row.pay"   :min="0" :precision="2" @change="itemChange"></a-input-number>
                    <nk-a-input-percent          v-if="row._def.rate"  v-model="row.rate"  :min="0" :precision="4" @change="itemChange"></nk-a-input-percent>
                    <a-input-number size="small" v-if="row._def.value" v-model="row.value" :min="0" :precision="0" @change="itemChange"></a-input-number>
                </template>
            </vxe-column>
            <vxe-column title="">
                <template v-slot="{seq,items}">
                    <span v-if="editMode" class="drag-btn" style="margin-left: 10px;">
                        <a-icon type="swap" :rotate="90" />
                    </span>
                    <span v-if="editMode&&!def.disabledRemove" style="margin-left: 10px;" @click="xTableRemove(data,seq)">
                        <a-icon type="delete" />
                    </span>
                </template>
            </vxe-column>
        </vxe-table>
    </nk-card>
</template>

<script>
    import Mixin from "Mixin";
    import MixinSortable from "MixinSortable";
    import moment from "moment";
    export default {
        mixins:[new Mixin({}),new MixinSortable()],
        data(){
            return {
                calcLock: false,
                activedDef:{},
                trigger: false,
            }
        },
        computed:{
            options(){
                return this.def && this.def.programDefs && this.def.programDefs.map(i=>{
                    return {
                        label: i.name,
                        value: i.key
                    }
                });
            },
            list(){
                return this.data && this.data.map(item=>{
                    if(this.def && this.def.programDefs && item.program){
                        item._def = this.def.programDefs.find(d=>d.key === item.program)||{}
                    }else{
                        item._def = {}
                    }
                    return item;
                });
            }
        },
        methods:{
            docUpdate(){
                this.calcLock = false;
            },
            xTableEditClosed(){
                if(this.trigger&&!this.calcLock){
                    this.calcLock = true;
                    this.trigger = false;
                    this.nk$calc();
                }
            },
            xTableRemove(data,seq){
                this.$nkSortableRemove(data,seq);
                this.trigger = true;
                this.xTableEditClosed();
            },
            async xTableAdd(){
                let row = {
                    program: this.def && this.def.programDefs && this.def && this.def.programDefs[0].key,
                    activeDate:moment().unix()
                };
                this.data.push(row);
                this.trigger = true;
                await this.$refs.xTable.loadData(this.data);
                await this.$refs.xTable.setActiveRow(row);
                await this.$refs.xTable.validate(row).catch(errMap => errMap)
            },
            xTableEditActived({row}){
                this.activedDef = row._def;
            },
            programChange({row}){
                if(this.def && this.def.programDefs && row.program){
                    this.activedDef = this.def.programDefs.find(d=>d.key === row.program)||{}
                }else{
                    this.activedDef = {}
                }
                this.trigger = true;
            },
            itemChange(){
                this.trigger = true;
            }
        }
    }
</script>

<style scoped>

</style>